/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db.JDBC.core;

import core.*;
import core.comparators.ComparatorFactory;
import core.comparators.SchoolComparator.SCHOOLCOMPARATORTYPE;
import db.JDBC.*;
import db.core.interfaces.SchoolDAOCore;
import db.interfaces.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
/**
 *
 * @author Anton
 */
public class SchoolJdbcDAOCore implements SchoolDAOCore
{
    private static SchoolJdbcDAOCore instance = null;
    private SchoolDAO schoolDAO;
    private CRUDJdbcDAOCore crud;

    /**
     *
     */
    private SchoolJdbcDAOCore()
    {
        JdbcDaoFactory daoFactory = JdbcDaoFactory.getInstance();
        schoolDAO = daoFactory.getSchoolJdbcDAO();
        crud = CRUDJdbcDAOCore.getInstance();
    }

    /**
     * 
     * @return
     */
    public static SchoolJdbcDAOCore getInstance()
    {
        if(instance == null)
            instance = new SchoolJdbcDAOCore();
        return instance;
    }

    /**
     * 
     * @param id
     * @return
     */
    public School getSchoolById(long id)
    {
        return crud.getSchool(id);
    }

    /**
     *
     * @param beginsWith
     * @param maxResult
     * @return
     */
    public Collection<String> searchForStringSchools(String beginsWith, int maxResult)
    {
        Collection<School> names  = schoolDAO.searchForSchoolsByName(beginsWith);
        Collection<String> t = new TreeSet<String>();
        List<String> returnCollection = new ArrayList<String>();
        if(names != null)
            for(School s : names)
                if(s != null)
                    t.add(s.getName());

        for(String s : t)
            if(s != null)
                returnCollection.add(s);
        
        if(returnCollection.size() < maxResult)
            maxResult = returnCollection.size();
        return returnCollection.subList(0, maxResult);
    }
    /**
     *
     * @param beginsWith
     * @return
     */
    public Collection<School> searchForObjectSchools(String beginsWith)
    {
        Collection<School> names  = schoolDAO.searchForSchoolsByName(beginsWith);
        Collection<School> t = new TreeSet<School>(ComparatorFactory.getSchoolComparator(SCHOOLCOMPARATORTYPE.ID, true));
        List<School> returnCollection = new ArrayList<School>();
        if(names != null)
            for(School s : names)
                if(s != null)
                    t.add(s);
        for(School s : t)
            if(s != null)
                returnCollection.add(s);
        Collections.sort(returnCollection,ComparatorFactory.getSchoolComparator(SCHOOLCOMPARATORTYPE.NAME, true));

        return returnCollection;
    }

    /**
     * 
     * @param name
     * @return
     */
    public Collection<School> getSchoolsByName(String name)
    {
        if(name == null)
            return null;
        Collection<School> schools = new ArrayList<School>();
        for(School s : schoolDAO.getSchoolsByName(name))
            if(s != null)
                schools.add(crud.getSchool(s.getId()));
        return schools;
    }
    /**
     * 
     * @return
     */
    public Collection<School> getAllSchools()
    {
        Collection<School> schools = new ArrayList<School>();
        for(School s : schoolDAO.getAllSchools())
            if(s != null)
                schools.add(crud.getSchool(s.getId()));
        return schools;        
    }

    /**
     * 
     * @param school
     * @param program
     */
    public void addProgramToSchool(School school, Program program)
    {
        if(school == null || program == null)
            return;
        crud.addProgramToSchool(school, program);
    }
    /**
     * 
     * @param school
     * @param program
     */
    public void deleteProgramFromSchool(School school, Program program)
    {
        if(school == null || program == null)
            return;
        crud.deleteProgramFromSchool(school, program);        
    }

    /**
     * 
     * @param school
     * @param course
     */
    public void addCourseToSchool(School school, Course course)
    {
        if(school == null || course == null)
            return;
        crud.addCourseToSchool(school, course);
    }
    /**
     * 
     * @param school
     * @param course
     */
    public void deleteCourseFromSchool(School school, Course course)
    {
        if(school == null || course == null)
            return;
        crud.deleteCourseFromSchool(school, course);
    }

    /**
     * 
     * @param school
     */
    public void addSchool(School school)
    {
        if(school == null)
            return;
        crud.addSchool(school);
    }
    /**
     * 
     * @param school
     */
    public void updateSchool(School school)
    {
        if(school == null)
            return;
        crud.updateSchool(school);
    }
    /**
     * 
     * @param school
     */
    public void deleteSchool(School school)
    {
        if(school == null)
            return;
        crud.deleteSchool(school);
    }
}
